#include<stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include<stdlib.h>
#include<errno.h>
#include<string.h>

int main()
{
    int pid=fork();
    if(pid==0)
    {
        printf("我是子进程，pid:%d,ppid:%d\n",getpid(),getppid());
        //pid/=0;
        exit(1);
    }
    sleep(3);
    int status=0;
    pid_t rid=waitpid(pid,&status,0);
    //waitpid正常
    if(rid>0)
    {
        printf("wait success\n");
        //程序异常退出(崩溃)，终止信号>0
        if((status&0x7f)>0)
        {
            printf("子进程退出异常：signal code:%d\n",status&0x7f);
        }
        //程序正常退出,终止信号==0
        else{
            printf("子进程正常退出exit code:%d\n",(status>>8)&0xff);
        }       
    }
    //waitpid失败
    else{
        printf("wait fail:%d:%s\n",errno,strerror(errno));
    }
    return 0;

}

